var ECharts;
$(document).ready(function () {

    require.config({
        baseUrl: ctx + '/resource/plugins/echarts4',
        paths: {
            echarts: 'echarts',
            roma: 'theme/roma',
        }
    });
    require(
        [
            'echarts',
            'roma'
        ],
        function (ec) {
            ECharts = ec;
            cpu_h = ECharts.init(document.getElementById('cpu_h'), 'roma');//CPU使用率折线图
            contrastCPU();
            cpu_num = ECharts.init(document.getElementById('cpu_num'), 'roma');//CPU使用率柱状图
            queryCPUNum();
            memory_h = ECharts.init(document.getElementById('memory_h'), 'roma');//内存使用率折线图
            contrastMemory();
            memory_num = ECharts.init(document.getElementById('memory_num'), 'roma');//内存使用率柱状图
            queryMemoryNum();

        }
    );
});

function contrastCPU() {
    $.ajax({
        url: ctx + "/queryTime/Time",
        type: "post",
        data: "EXT_UNIT_ID=" + EXT_UNIT_ID + "&KPI_CODE=" + "PM-00-02-001-06" + "&START=" + START + "&END=" + END + "&SELECT=" + SELECT,
        success: function (data) {
            var list = JSON.parse(data);
            var xdata = [];
            var ydata1 = [];
            var x_value = [];
            if (SELECT == "showTime") {
                for (var i = 0; i < list.length; i++) {
                    x_value = list[i].DATA_TIME.substring(list[i].DATA_TIME.lastIndexOf(" ") + 1);
                    xdata.push(x_value);
                    ydata1.push(list[i].AVG_VALUE);
                }
            } else if (SELECT == "showMonth") {
                for (var i = 0; i < list.length; i++) {
                    // x_value = list[i].DATA_TIME.split("-");
                    // xdata.push(x_value[2]);
                    x_value=list[i].DATA_TIME;
                    xdata.push(x_value);
                    ydata1.push(list[i].AVG_VALUE);
                }
            } else {
                for (var i = 0; i < list.length; i++) {
                    xdata.push(list[i].DATA_TIME);
                    ydata1.push(list[i].AVG_VALUE);
                }
            }
            option = {
                tooltip: {
                    trigger: 'axis',
                },
                legend: {
                    data: ['CPU使用率'],
                    textStyle: {
                        color: '#AAAAAA'
                    }
                },
                grid: {
                    left: '3%',
                    right: '4%',
                    bottom: '3%',
                    containLabel: true
                },
                xAxis: [{
                    type: 'category',
                    boundaryGap: false,
                    axisLabel: {
                        /*formatter:'{value} h',*/
                        textStyle: {
                            color: '#AAAAAA'
                        }
                    },
                    data: xdata
                }],
                yAxis: [{
                    type: 'value',
                    max: 100,//Y轴最大值 不写的话自动调节
                    axisLabel: {
                        formatter: '{value} %',
                        textStyle: {
                            color: '#AAAAAA'
                        }
                    }
                }],
                series: [
                    {
                        name: 'CPU使用率',
                        type: 'line',
                        color: '#EFB700',
                        data: ydata1
                    },
                ]
            };
            cpu_h.setOption(option);
        }, error: function (textStatus) {
            console.log("error:" + textStatus.status);
        }
    });
}

function contrastMemory() {
    $.ajax({
        url: ctx + "/queryTime/Time",
        type: "post",
        data: "EXT_UNIT_ID=" + EXT_UNIT_ID + "&KPI_CODE=" + "PM-00-02-001-07" + "&START=" + START + "&END=" + END + "&SELECT=" + SELECT,
        success: function (data) {
            var list = JSON.parse(data);
            var xdata = [];
            var ydata1 = [];
            var x_value = [];
            if (SELECT == "showTime") {
                for (var i = 0; i < list.length; i++) {
                    x_value = list[i].DATA_TIME.substring(list[i].DATA_TIME.lastIndexOf(" ") + 1);
                    xdata.push(x_value);
                    ydata1.push(list[i].AVG_VALUE);
                }
            } else if (SELECT == "showMonth") {
                for (var i = 0; i < list.length; i++) {
                    x_value=list[i].DATA_TIME;
                    xdata.push(x_value);
                    ydata1.push(list[i].AVG_VALUE);
                }
            } else {
                for (var i = 0; i < list.length; i++) {
                    xdata.push(list[i].DATA_TIME);
                    ydata1.push(list[i].AVG_VALUE);
                }
            }
            option = {
                tooltip: {
                    trigger: 'axis',
                },
                legend: {
                    data: ['内存使用率'],
                    textStyle: {
                        color: '#AAAAAA'
                    }
                },
                grid: {
                    left: '3%',
                    right: '4%',
                    bottom: '3%',
                    containLabel: true
                },
                xAxis: [{
                    type: 'category',
                    boundaryGap: false,
                    axisLabel: {
                        /*formatter:'{value} h',*/
                        textStyle: {
                            color: '#AAAAAA'
                        }
                    },
                    data: xdata
                }],
                yAxis: [{
                    type: 'value',
                    max: 100,//Y轴最大值 不写的话自动调节
                    axisLabel: {
                        formatter: '{value} %',
                        textStyle: {
                            color: '#AAAAAA'
                        }
                    }
                }],
                series: [
                    {
                        name: '内存使用率',
                        type: 'line',
                        color: '#EFB700',
                        data: ydata1
                    },
                ]
            };
            memory_h.setOption(option);
        }, error: function (textStatus) {
            console.log("error:" + textStatus.status);
        }
    });
}

function queryCPUNum(){
    $.ajax({
        url: ctx + "/queryTime/queryNum",
        type: "post",
        data: "EXT_UNIT_ID=" + EXT_UNIT_ID + "&KPI_CODE=" + "PM-00-02-001-06" + "&START=" + START + "&END=" + END + "&SELECT=" + SELECT,
        success: function (data) {
            var memoryNum = JSON.parse(data);
            var xdata = [];
            var num1 = '';
            var min_value = '';
            var max_value = '';
            var group = '';
            var p = [];
            var values = [];
            var result = [0, 0, 0, 0, 0];
            var temp = '';
            var temp1 = '';
            var hkh = '';
            var myMap = new Map();
            if (SELECT == "showTime" || SELECT == "showDay") {//，分割分完的截取每个元素的倒数第一个或者第二个双引号的值{"0.0~20.0":"289"，"20.0~40.0":"290，"40.0~60.0":"291"}
                for (var i = 0; i < memoryNum.length; i++) {
                    hkh = memoryNum[i].GROUP_VALUE.substring(1, memoryNum[i].GROUP_VALUE.lastIndexOf('}'));
                    num1 = hkh.split(',');
                    min_value = memoryNum[i].Y_MIN_VALUE;
                    max_value = memoryNum[i].Y_MAX_VALUE;
                    group = memoryNum[i].VALUE_GROUP;
                    for (j = 0; j < num1.length; j++) {
                        temp = num1[j].substring(num1[j].lastIndexOf(':"') + 2, num1[j].lastIndexOf('"'));
                        temp1 = num1[j].substring(1, num1[j].lastIndexOf('":'));
                        myMap.set(temp1, temp);
                    }
                    if (myMap.get("0.0~20.0") != null) {
                        xdata.push(myMap.get("0.0~20.0"));
                    } else {
                        xdata.push(0);
                    }
                    if (myMap.get("20.0~40.0") != null) {
                        xdata.push(myMap.get("20.0~40.0"));
                    } else {
                        xdata.push(0);
                    }
                    if (myMap.get("40.0~60.0") != null) {
                        xdata.push(myMap.get("40.0~60.0"));
                    } else {
                        xdata.push(0);
                    }
                    if (myMap.get("60.0~80.0") != null) {
                        xdata.push(myMap.get("60.0~80.0"));
                    } else {
                        xdata.push(0);
                    }
                    if (myMap.get("80.0~100.0") != null) {
                        xdata.push(myMap.get("80.0~100.0"));
                    } else {
                        xdata.push(0);
                    }
                }
            } else {
                for (var i = 0; i < memoryNum.length; i++) {
                    min_value = memoryNum[i].Y_MIN_VALUE;
                    max_value = memoryNum[i].Y_MAX_VALUE;
                    group = memoryNum[i].VALUE_GROUP;
                    p = (memoryNum[i].GROUP_VALUE).split(',');
                    for (var j = 0; j < p.length; j++) {
                        p[j] = parseInt(p[j].substring(p[j].lastIndexOf(':"') + 2, p[j].lastIndexOf('"')));
                    }
                    values.push(p);
                }
                for (var k = 0; k < 5; k++) {                //需要一共循环个数 5次 因为结果个数为5
                    for (i = 0; i < values.length; i++) {           //循环二维数组  结果元素个数次
                        result[k] = result[k] + values[i][k]
                    }
                }
                xdata = result;
            }
            option = {
                color: ['#23CBFE'],
                title: {
                    text: '',
                    subtext: ''
                },
                tooltip: {
                    trigger: 'axis',
                    axisPointer: {
                        type: 'shadow'
                    }
                },
                legend: {
                    data: ['CPU使用率次数'],
                    textStyle: {
                        color: '#AAAAAA'
                    }
                },
                grid: {
                    left: '3%',
                    right: '4%',
                    bottom: '3%',
                    containLabel: true
                },
                xAxis: {
                    type: 'value',
                    boundaryGap: [0, 0.01],
                    splitLine:{
                        show:false
                    },
                    axisLabel: {
                        textStyle: {
                            color: '#AAAAAA'
                        }
                    }
                },
                yAxis: {
                    type: 'category',
                    splitLine:{
                        show:true
                    },
                    axisLabel: {
                        /* formatter:'{value} %',*/
                        textStyle: {
                            color: '#AAAAAA'
                        }
                    },
                    data:[
                        (max_value-min_value)/group,
                        (max_value-min_value)/group*2,
                        (max_value-min_value)/group*3,
                        (max_value-min_value)/group*4,
                        (max_value-min_value)/group*5,
                    ]
                },
                series: [
                    {
                        name: 'CPU使用率次数',
                        type: 'bar',
                        barWidth : 30,
                        data: xdata
                    }
                ]

            };
            cpu_num.setOption(option);
        },error:function(textStatus){
            console.log("error:"+textStatus.status);
        }
    });
}

function queryMemoryNum(){
    $.ajax({
        url: ctx + "/queryTime/queryNum",
        type: "post",
        data: "EXT_UNIT_ID=" + EXT_UNIT_ID + "&KPI_CODE=" + "PM-00-02-001-07" + "&START=" + START + "&END=" + END + "&SELECT=" + SELECT,
        success: function (data) {
            var memoryNum = JSON.parse(data);
            var xdata = [];
            var num1 = '';
            var min_value = '';
            var max_value = '';
            var group = '';
            var p = [];
            var values = [];
            var result = [0, 0, 0, 0, 0];
            var temp = '';
            var temp1 = '';
            var hkh = '';
            var myMap = new Map();
            if (SELECT == "showTime" || SELECT == "showDay") {//，分割分完的截取每个元素的倒数第一个或者第二个双引号的值{"0.0~20.0":"289"，"20.0~40.0":"290，"40.0~60.0":"291"}
                for (var i = 0; i < memoryNum.length; i++) {
                    hkh = memoryNum[i].GROUP_VALUE.substring(1, memoryNum[i].GROUP_VALUE.lastIndexOf('}'));
                    num1 = hkh.split(',');
                    min_value = memoryNum[i].Y_MIN_VALUE;
                    max_value = memoryNum[i].Y_MAX_VALUE;
                    group = memoryNum[i].VALUE_GROUP;
                    for (j = 0; j < num1.length; j++) {
                        temp = num1[j].substring(num1[j].lastIndexOf(':"') + 2, num1[j].lastIndexOf('"'));
                        temp1 = num1[j].substring(1, num1[j].lastIndexOf('":'));
                        myMap.set(temp1, temp);
                    }
                    if (myMap.get("0.0~20.0") != null) {
                        xdata.push(myMap.get("0.0~20.0"));
                    } else {
                        xdata.push(0);
                    }
                    if (myMap.get("20.0~40.0") != null) {
                        xdata.push(myMap.get("20.0~40.0"));
                    } else {
                        xdata.push(0);
                    }
                    if (myMap.get("40.0~60.0") != null) {
                        xdata.push(myMap.get("40.0~60.0"));
                    } else {
                        xdata.push(0);
                    }
                    if (myMap.get("60.0~80.0") != null) {
                        xdata.push(myMap.get("60.0~80.0"));
                    } else {
                        xdata.push(0);
                    }
                    if (myMap.get("80.0~100.0") != null) {
                        xdata.push(myMap.get("80.0~100.0"));
                    } else {
                        xdata.push(0);
                    }
                }
            } else {
                for (var i = 0; i < memoryNum.length; i++) {
                    min_value = memoryNum[i].Y_MIN_VALUE;
                    max_value = memoryNum[i].Y_MAX_VALUE;
                    group = memoryNum[i].VALUE_GROUP;
                    p = (memoryNum[i].GROUP_VALUE).split(',');
                    for (var j = 0; j < p.length; j++) {
                        p[j] = parseInt(p[j].substring(p[j].lastIndexOf(':"') + 2, p[j].lastIndexOf('"')));
                    }
                    values.push(p);
                }
                for (var k = 0; k < 5; k++) {                //需要一共循环个数 5次 因为结果个数为5
                    for (i = 0; i < values.length; i++) {           //循环二维数组  结果元素个数次
                        result[k] = result[k] + values[i][k]
                    }
                }
                xdata = result;
            }
            option = {
                color: ['#23CBFE'],
                title: {
                    text: '',
                    subtext: ''
                },
                tooltip: {
                    trigger: 'axis',
                    axisPointer: {
                        type: 'shadow'
                    }
                },
                legend: {
                    data: ['内存使用率次数'],
                    textStyle: {
                        color: '#AAAAAA'
                    }
                },
                grid: {
                    left: '3%',
                    right: '4%',
                    bottom: '3%',
                    containLabel: true
                },
                xAxis: {
                    type: 'value',
                    boundaryGap: [0, 0.01],
                    axisLabel: {
                        textStyle: {
                            color: '#AAAAAA'
                        }
                    },
                    splitLine:{
                        show:false
                    }
                },
                yAxis: {
                    type: 'category',
                    axisLabel: {
                        textStyle: {
                            color: '#AAAAAA'
                        }
                    },
                    splitLine:{
                        show:true
                    },
                    data:[
                        (max_value-min_value)/group,
                        (max_value-min_value)/group*2,
                        (max_value-min_value)/group*3,
                        (max_value-min_value)/group*4,
                        (max_value-min_value)/group*5,
                    ]
                },
                series: [
                    {
                        name: '内存使用率次数',
                        type: 'bar',
                        barWidth : 30,
                        data: xdata
                    }
                ]
            };
            memory_num.setOption(option);
        },error:function(textStatus) {
            console.log("error:" + textStatus.status);
        }
    });
}
